package com.rfid.assistant;

import android.text.TextUtils;

import com.rfid.assistant.bean.DoorConfig;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CmdInOutPutStream {

    private OutputStream mOutStream;
    private InputStream mInput;
    public CmdInOutPutStream(InputStream in, OutputStream outputStream) {
        mOutStream = outputStream;
        mInput = in;
    }

    public void hello(String msg) throws IOException {
        mOutStream.write(0xf8);
        int len = msg.length();
        mOutStream.write(len);
        mOutStream.write(msg.getBytes());
        mOutStream.write(-1);
    }

    public void readHello() throws IOException {
        int cmd = mInput.read();
        System.out.println("read cmd = " + cmd);
        if(cmd != 0xf8) {
            return;
        }
        int len = mInput.read();
        byte buffer[] = new byte[len];
        int tl = 0;
        while(tl != len) {
            tl = tl + mInput.read(buffer,tl,len - tl);
        }
        System.out.println(new String(buffer));
    }

    public void read() throws IOException {
        int cmd = mInput.read();
        if(cmd == DoorConfig.CMD_READ_CONFIG) {
            DoorConfig config = new DoorConfig();
            config.setServerIp("192.168.1.100");
            config.setWorkMode(2);
            config.setWorkMode(2);
            config.setVol(3);
            config.setPower(6);
            config.setIp("192.168.1.102");
            config.setNetWorkMode(1);
            config.setAfiP(1);
            config.setAfi(2);
            config.setDoorCount(5);
            config.setInOut(1);
            config.setDoorAddr(7);
            mOutStream.write(DoorConfig.CMD_READ_CONFIG);
            config(config);
        }
    }

    public void config(DoorConfig config) throws IOException {
        mOutStream.write(DoorConfig.CMD_WRITE_CONFIG);
        if(config.getVol() > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_VOL);
            mOutStream.write(config.getVol());
        }
        if(config.getWorkMode() > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_WORK_MODE);
            mOutStream.write(config.getWorkMode());
        }
        int warnMode = config.getWarnMode();
        int doorAddr = config.getDoorAddr();
        int afi = config.getAfi();
        int doorCount = config.getDoorCount();
        int netWorkMode = config.getNetWorkMode();
        int inOut = config.getInOut();
        String ip = config.getIp();
        String serverIp = config.getServerIp();
        int afiP = config.getAfiP();
        int power = config.getPower();
        if(warnMode > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_WARN_MODE);
            mOutStream.write(warnMode);
        }
        if(doorAddr > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_DOOR_ADDR);
            mOutStream.write(doorAddr);
        }
        if(afi > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_AFI);
            mOutStream.write(afi);
        }
        if(doorCount > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_DOOR_COUNT);
            mOutStream.write(doorCount);
        }
        if(netWorkMode > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_NET_WORK_MODE);
            mOutStream.write(netWorkMode);
        }
        if(inOut > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_INOUT);
            mOutStream.write(inOut);
        }
        if(afiP > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_AFIP);
            mOutStream.write(afiP);
        }
        if(power > 0) {
            mOutStream.write(DoorConfig.SUB_CMD_POWER);
            mOutStream.write(power);
        }
        if(!TextUtils.isEmpty(ip)) {
            mOutStream.write(DoorConfig.SUB_CMD_IP);
            int len = ip.getBytes().length;
            mOutStream.write(len);
            mOutStream.write(ip.getBytes());
        }
        if(!TextUtils.isEmpty(serverIp)) {
            mOutStream.write(DoorConfig.SUB_CMD_SERVER_IP);
            int len = serverIp.getBytes().length;
            mOutStream.write(len);
            mOutStream.write(serverIp.getBytes());
        }
        mOutStream.write(0XFF);
    }

    public DoorConfig readDoorConfig() throws IOException {
        mOutStream.write(DoorConfig.CMD_READ_CONFIG);
        System.out.println("after readDoorConfig cmd");
        int cmd = mInput.read();
        System.out.println(" readDoorConfig subcmd = " + cmd);
        if(cmd != DoorConfig.CMD_READ_CONFIG) {
            return null;
        }
        DoorConfig config = new DoorConfig();
        int subcmd =0;
        while((subcmd = mInput.read()&0XFF) != 0xFF) {
            readValue(config,subcmd);
        }
        return config;
    }

    private void readValue(DoorConfig config,int subcmd) throws IOException {
        System.out.println("subcmd = " + subcmd);
        switch (subcmd) {
            case DoorConfig.SUB_CMD_AFI:
                config.setAfi(mInput.read());
                break;
            case DoorConfig.SUB_CMD_AFIP:
                config.setAfiP(mInput.read());
                break;
            case DoorConfig.SUB_CMD_DOOR_ADDR:
                config.setDoorAddr(mInput.read());
                break;
            case DoorConfig.SUB_CMD_DOOR_COUNT:
                config.setDoorCount(mInput.read());
                break;
            case DoorConfig.SUB_CMD_INOUT:
                config.setInOut(mInput.read());
                break;
            case DoorConfig.SUB_CMD_IP:
                int len = mInput.read();
                byte buffer[] = new byte[len];
                int tl = 0;
                while(tl != len) {
                    tl = tl + mInput.read(buffer,tl,len - tl);
                }
                config.setIp(new String(buffer));
                break;
            case DoorConfig.SUB_CMD_NET_WORK_MODE:
                config.setNetWorkMode(mInput.read());
                break;
            case DoorConfig.SUB_CMD_POWER:
                config.setPower(mInput.read());
                break;
            case DoorConfig.SUB_CMD_VOL:
                config.setVol(mInput.read());
                break;
            case DoorConfig.SUB_CMD_WARN_MODE:
                config.setWarnMode(mInput.read());
                break;
            case DoorConfig.SUB_CMD_WORK_MODE:
                config.setWorkMode(mInput.read());
                break;
            case DoorConfig.SUB_CMD_SERVER_IP:
                len = mInput.read();
                buffer = new byte[len];
                tl = 0;
                while(tl != len) {
                    tl = tl + mInput.read(buffer,tl,len - tl);
                }
                config.setServerIp(new String(buffer));
            default:
                break;
        }
    }
}
